package com.zretc.filter;

import java.io.IOException;
import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import com.zretc.util.WordsUtil;
/**
 * Servlet Filter implementation class SensitiveWordsFilter
 */
/*@WebFilter("/SensitiveWordsFilter")*/
public class SensitiveWordsFilter implements Filter {

    /**
     * Default constructor. 
     */
    public SensitiveWordsFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		MyRequest req = new MyRequest((HttpServletRequest)request);
		chain.doFilter(req, response);
	}

	
	/**
	 * @see Filter#init(FilterConfig)
	 */
	@Override
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}

class MyRequest extends HttpServletRequestWrapper{
    public MyRequest(HttpServletRequest request) {
        super(request);
    }
   @Override
    public String getParameter(String name) {
        String str = super.getParameter(name);
        List<String> list = WordsUtil.getWords();
        if (str!=null) {
        	 for(String word : list){
                str = str.replaceAll(word, "*");
            }
		}
        return str;
    }
}